<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use Illuminate\Support\Facades\Hash;    //引入hash
use App\Services\Cache\AdminCacheService;   //admin缓存
use App\Services\Tools\JwtService;    //引入JWT服务
use App\Services\Tools\FileService;   //引入文件处理类
use App\Response\Response;

class NobilityController extends BaseController
{

    /**
     * @api {post} /nobility/list 爵位列表
     * @apiVersion 0.1.0
     * @apiName 爵位列表
     * @apiGroup 爵位管理
     * @apiSuccessExample 成功响应
     *
     *{
        "code": 200,
        "data": {
            "list": [
                {
                    "id(主键)": "1",
                    "name(名字)": "子爵",
                    "price(价格)": "100,
                    "renew(续费)": "200",
                    "rebate(返利)": "100",
                    "head_picture(头像框图片)": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5efd8ac7b9463.png",
                    "moods(进场人气加成)": "100",
                    "ratio(经验加速%)": "1.2",
                    "image(爵位图片)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f0c4d057cc33.png",
                }
            ]
        }
    }
     *
     */
    public function nobilityList(Request $request)
    {
        $list = app('repo_nobility')->NobilityList();
        return Response::sendData($list);
    }



    /**
     * @api {post} /nobility/add  添加爵位
     * @apiVersion 0.1.0
     * @apiName 添加爵位
     * @apiGroup 爵位管理
     *
     * @apiParam {string} name 名称,必传
     * @apiParam {string} price 价格,必传
     * @apiParam {string} renew 续费价格,必传
     * @apiParam {string} rebate 返利,非必传
     * @apiParam {string} moods 人气加成,非必传
     * @apiParam {string} ratio 经验加速,非必传
     * @apiParam {string} head_picture  爵位图标url,必传
     * @apiParam {string} image  爵位图片url,必传
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 93013  爵位添加失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93013,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function nobilityAdd(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'name' => 'required|string',
                'price' => 'required|numeric|integer|min:0|max:2100000000',
                'renew' => 'required|numeric|integer|min:0|max:2100000000',
                'rebate' => 'filled|numeric|integer|min:0|max:2100000000',
                'moods' => 'filled|numeric|integer|min:0|max:2100000000',
                'ratio' => 'filled|numeric|min:0',
                'head_picture' => 'required',
                'image' => 'required',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        if (strstr($request->input('ratio'), '.')) {
            if (strlen($request->input('ratio')) > 5) {
                throw new BasicException(10001, '经验加速字符长度不能大于5位');
            }
        } else {
            if (strlen($request->input('ratio')) > 3) {
                throw new BasicException(10001, '经验加速字符长度不能大于3位');
            }
        }
        $name = $request->input('name');
        $price = $request->input('price');
        $renew = $request->input('renew',0);
        $rebate = $request->input('rebate',0);
        $moods = $request->input('moods',10);
        $ratio = $request->input('ratio',0);
        $headPicture = $request->input('head_picture');
        $image = $request->input('image');

        //数据组合
        $param['name']=$name;
        $param['price']=$price;
        $param['renew']=$renew;
        $param['rebate']=$rebate;
        $param['moods']=$moods;
        $param['ratio']=$ratio;
        $param['head_picture'] = $headPicture;
        $param['image'] = $image;
        //执行添加操作
        app('repo_nobility')->nobilityAdd($param);
        return Response::sendData(true);
    }



    /**
     * @api {post} /nobility/save  编辑爵位
     * @apiVersion 0.1.0
     * @apiName 编辑爵位
     * @apiGroup 爵位管理
     *
     * @apiParam {number} id 主键,必传
     * @apiParam {string} name 名称,必传
     * @apiParam {string} price 价格,必传
     * @apiParam {string} renew 续费价格,必传
     * @apiParam {string} rebate 返利,必传
     * @apiParam {string} moods 人气加成,必传
     * @apiParam {string} ratio 经验加速,必传
     * @apiParam {string} head_picture  爵位图标url,必传
     * @apiParam {string} image  爵位图片url,必传
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 93014  爵位编辑失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93014,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function nobilitySave(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|numeric',
                'name' => 'required|string',
                'price' => 'required|numeric|integer|min:0|max:2100000000',
                'renew' => 'required|numeric|integer|min:0|max:2100000000',
                'rebate' => 'filled|numeric|integer|min:0|max:2100000000',
                'moods' => 'filled|numeric|integer|min:0|max:2100000000',
                'ratio' => 'filled|numeric|min:0',
                'head_picture' => 'required',
                'image' => 'required',
            ],
            [
                'name.required' => '名字不能为空',
                'name.string' => '名字必须为字符串',
                'price.required' => '价格不能为空',
                'price.numeric' => '价格必须为数字',
                'price.integer' => '价格必须为整数',
                'price.min' => '价格最小值为0',
                'price.max' => '价格字符最大值为2100000000',
                'renew.required' => '续费不能为空',
                'renew.numeric' => '续费必须为数字',
                'renew.integer' => '续费必须为整数',
                'renew.min' => '续费最小值为0',
                'renew.max' => '续费字符最大值为2100000000',
                'rebate.filled' => '返利不能为空',
                'rebate.numeric' => '返利必须为数字',
                'rebate.integer' => '返利必须为整数',
                'rebate.min' => '返利最小值为0',
                'rebate.max' => '返利字符最大值为2100000000',
                'moods.filled' => '进场人气加成不能为空',
                'moods.numeric' => '进场人气加成必须为数字',
                'moods.integer' => '进场人气加成必须为整数',
                'moods.min' => '进场人气加成最小值为0',
                'moods.max' => '进场人气加成最大值为2100000000',
                'ratio.filled' => '经验加速不能为空',
                'ratio.numeric' => '经验加速必须为数字',
                'ratio.min' => '经验加速最小值为0',
                'head_picture.required' => '请上传爵位图片',
                'image.required' => '请上传爵位图片',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());
        if (strstr($request->input('ratio'), '.')) {
            if (strlen($request->input('ratio')) > 5) {
                throw new BasicException(10001, '经验加速字符长度不能大于5位');
            }
        } else {
            if (strlen($request->input('ratio')) > 3) {
                throw new BasicException(10001, '经验加速字符长度不能大于3位');
            }
        }
        $name = $request->input('name');
        $id = $request->input('id');
        $price = $request->input('price');
        $renew = $request->input('renew',0);
        $rebate = $request->input('rebate',0);
        $moods = $request->input('moods',10);
        $ratio = $request->input('ratio',0);
        $headPicture = $request->input('head_picture');
        $image = $request->input('image');

        //数据组合
        $param['name']=$name;
        $param['price']=$price;
        $param['renew']=$renew;
        $param['rebate']=$rebate;
        $param['moods']=$moods;
        $param['ratio']=$ratio;
        $param['id']=$id;
        $param['head_picture'] = $headPicture;
        $param['image'] = $image;
        //执行编辑操作
        app('repo_nobility')->nobilitySave($param);
        return Response::sendData(true);
    }




    /**
     * @api {post} /nobility/del  删除爵位
     * @apiVersion 0.1.0
     * @apiName 删除爵位
     * @apiGroup 爵位管理
     * @apiParam {string} ids 主键，多个用逗号隔开,必传
     * @apiSuccessExample 成功响应
     *
     *      {"code":200,"data":true}
     *
     * @apiError 93015  爵位删除失败
     *
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93015,
     *          "message": "errorMsg"
     *      }
     *
     */
    public function nobilityDel(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $ids = $request->input('ids');
        $param = explode(',', $ids);
        //执行添加操作
        app('repo_nobility')->nobilityDel($param);
        return Response::sendData(true);
    }


    /**
     * @api {post} /nobility/detail  爵位详情
     * @apiVersion 0.1.0
     * @apiName 爵位详情
     * @apiGroup 爵位管理
     * @apiParam {string} id 主键,必传
     * @apiSuccessExample 成功响应
     *
     * {
            "code": 200,
            "data": {
                "id(主键)": "1",
                "name(名字)": "子爵",
                "price(价格)": "100,
                "renew(续费)": "200",
                "rebate(返利)": "100",
                "head_picture(头像框图片)": "http://yutangyuyin.oss-cn-hangzhou.aliyuncs.com/admin_images/5efd8ac7b9463.png",
                "moods(进场人气加成)": "100",
                "ratio(经验加速%)": "1.2",
                "image(爵位图片)": "http://yutangyuyin1.oss-cn-hangzhou.aliyuncs.com/admin_images/5f0c4d057cc33.png",
            }
    }
     *
     */
    public function detail(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'id' => 'required|numeric',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $id = $request->input('id');
        //执行查询操作
        $detail = app('repo_nobility')->nobilityDetail($id);
        return Response::sendData($detail);
    }






}
